﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Delonix_Regia_HMS.Classes;

namespace Delonix_Regia_HMS
{
    public partial class HousekeepingStaffManagement : Form
    {
        public HousekeepingStaffManagement()
        {
            InitializeComponent();
            DBManager.ListViewStaffDetailRecords(lvStaffDetailsRecord);
            DBManager.FillDropDownListRoomID(cbBoxDutyAllocationRoomID);
        }

        #region StaffDetail
        private void lvStaffDetailsRecord_SelectedIndexChanged(object sender, EventArgs e)
        {
            string bankAccNumber;
            if (lvStaffDetailsRecord.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in lvStaffDetailsRecord.SelectedItems)
                {
                    tbxStaffDetailsRecordStaffID.Text = lvStaffDetailsRecord.SelectedItems[0].Text;
                    tbxStaffDetailsRecordName.Text = lvStaffDetailsRecord.SelectedItems[0].SubItems[1].Text;
                    tbxStaffDetailsRecordDOB.Text = lvStaffDetailsRecord.SelectedItems[0].SubItems[2].Text;
                    bankAccNumber = lvStaffDetailsRecord.SelectedItems[0].SubItems[3].Text;
                    tbxStaffDetailsRecordBankACNo1.Text = bankAccNumber.Substring(0, 3);
                    tbxStaffDetailsRecordBankACNo2.Text = bankAccNumber.Substring(4, 5);
                    tbxStaffDetailsRecordBankACNo3.Text = bankAccNumber.Substring(6, 1);
                    tbxStaffDetailsRecordHomeAddress.Text = lvStaffDetailsRecord.SelectedItems[0].SubItems[4].Text;
                    tbxStaffDetailsRecordPhoneNumber.Text = lvStaffDetailsRecord.SelectedItems[0].SubItems[5].Text;
                    tbxStaffDetailsRecordStaffType.Text = lvStaffDetailsRecord.SelectedItems[0].SubItems[6].Text;
                }
            }
        }

        private void btnStaffDetailsRecordCreateStaff_Click(object sender, EventArgs e)
        {
            CreateStaff cs = new CreateStaff();
            cs.ShowDialog();
           
        }

        private void btnStaffDetailsRecordRefreshStaff_Click(object sender, EventArgs e)
        {
            // Refreshes the listview
            tbxStaffDetailsRecordSearchName.Clear();
            DBManager.ListViewStaffDetailRecords(lvStaffDetailsRecord);

        }

        private void btnStaffDetailsRecordUpdateStaff_Click(object sender, EventArgs e)
        {
            Staff s = new Staff();
            // Check if there's staff has been selected
            if (string.IsNullOrEmpty(tbxStaffDetailsRecordStaffID.Text))
            {
                MessageBox.Show("No Staff was selected");
            }
            else
            {
                // Check if required details are missing
                if (string.IsNullOrEmpty(tbxStaffDetailsRecordBankACNo1.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordBankACNo2.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordBankACNo3.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordHomeAddress.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordHomeAddress.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordPhoneNumber.Text))
                {
                    MessageBox.Show("Missing Information Required");
                }
                // If details are there
                else 
                {
                    int staffID = Convert.ToInt32(tbxStaffDetailsRecordStaffID.Text);
                    s.StaffBankAccountNumber = tbxStaffDetailsRecordBankACNo1.Text + "-" + tbxStaffDetailsRecordBankACNo2.Text + "-" + tbxStaffDetailsRecordBankACNo3.Text;
                    s.StaffHomeAddress = tbxStaffDetailsRecordHomeAddress.Text;
                    s.StaffHomeNumber = tbxStaffDetailsRecordPhoneNumber.Text;
                    DBManager.updateStaff(s, staffID);
                    MessageBox.Show("Successfull updated");
                    ClearTextBoxes(this.Controls);
                }
            }
            

        }

        #endregion

        #region DutyAllocation
        private void cbBoxDutyAllocationHouseKeeperSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            string selectedHousekeeper = cbBoxDutyAllocationHouseKeeperSearch.SelectedItem.ToString();
            string q1 = "Show All Housekeeper";
            string q2 = "Show All Available Housekeeper";
            string q3 = "Show All Unavailable Housekeeper";

            if (selectedHousekeeper == q1)
            {
                DBManager.ListViewListAllHousekeeper(lvDutyAllocationHouseKeeperAllocation);
            }
            else if (selectedHousekeeper == q2)
            {
                DBManager.ListViewListAllHousekeeperWithoutDuties(lvDutyAllocationHouseKeeperAllocation);
            }
            else if (selectedHousekeeper == q3)
            {
                DBManager.ListViewListAllHousekeeperWithDuties(lvDutyAllocationHouseKeeperAllocation);
            }
        }

        private void lvDutyAllocationHouseKeeperAllocation_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvDutyAllocationHouseKeeperAllocation.SelectedItems.Count > 0)
            {
                foreach (ListViewItem item in lvDutyAllocationHouseKeeperAllocation.SelectedItems)
                {
                    tbxDutyAllocationStaffID.Text = lvDutyAllocationHouseKeeperAllocation.SelectedItems[0].Text;
                    tbxDutyAllocationStaffName.Text = lvDutyAllocationHouseKeeperAllocation.SelectedItems[0].SubItems[1].Text;
                    tbxDutyAllocationCurrRoomID.Text = lvDutyAllocationHouseKeeperAllocation.SelectedItems[0].SubItems[2].Text;
                }
            }
        }

        private void btnDutyAllocationMakeDuties_Click(object sender, EventArgs e)
        {
            bool success = false;
            if (string.IsNullOrEmpty(tbxDutyAllocationStaffID.Text) || string.IsNullOrEmpty(cbBoxDutyAllocationDuties.Text) || string.IsNullOrEmpty(tbxDutyAllocationCurrRoomID.Text))
            {
                MessageBox.Show("Nothing Selected");
            }
            // Check to see whether a staffID has been selected or a duty has been selected
            else if (!string.IsNullOrEmpty(tbxDutyAllocationStaffID.Text) || !string.IsNullOrEmpty(cbBoxDutyAllocationDuties.Text))
            {
                HousekeepingRecords hkr = new HousekeepingRecords();
                Room r = new Room();

                int staffID = Convert.ToInt32(tbxDutyAllocationStaffID.Text);
                if (cbBoxDutyAllocationDuties.Text == "No Duties")
                {
                    hkr.RoomID = 0;
                    hkr.HousekeepingDateTime = DateTime.MaxValue;
                    hkr.Duties = cbBoxDutyAllocationDuties.Text;
                    DBManager.UpdateHouseKeeperDuties(hkr, staffID);
                    r.RoomStatus = "Vacant";
                    if (string.IsNullOrEmpty(tbxDutyAllocationCurrRoomID.Text))
                    {
                        r.RoomID = 0;
                    }
                    else
                    {
                        r.RoomID = Convert.ToInt32(tbxDutyAllocationCurrRoomID.Text);
                    }
                    DBManager.UpdateRoomStatus(r);
                    DBManager.FillDropDownListRoomID(cbBoxDutyAllocationRoomID);
                    success = true;                    
                }
                // Check to see if condition of the duty allocation is not "NO Duties"
                else if (cbBoxDutyAllocationDuties.Text != "No Duties")
                {
                    if (!string.IsNullOrEmpty(cbBoxDutyAllocationRoomID.Text))
                    {
                        // There's is no existing room that is being schedule for duty.
                        if(Convert.ToInt32(tbxDutyAllocationCurrRoomID.Text) == 0)
                        {
                            hkr.RoomID = Convert.ToInt32(cbBoxDutyAllocationRoomID.Text);
                            hkr.HousekeepingDateTime = Convert.ToDateTime(dtpDutyAllocationDate.Text);
                            hkr.Duties = cbBoxDutyAllocationDuties.Text;
                            DBManager.UpdateHouseKeeperDuties(hkr, staffID);
                            r.RoomStatus = "Scheduled for Duty";
                            r.RoomID = Convert.ToInt32(cbBoxDutyAllocationRoomID.SelectedItem.ToString());
                            DBManager.UpdateRoomStatus(r);
                            DBManager.FillDropDownListRoomID(cbBoxDutyAllocationRoomID);
                            success = true;
                        }
                        // There's is existing room, but needs to change one room to another. 
                        else if (Convert.ToInt32(tbxDutyAllocationCurrRoomID.Text) != 0)
                        {
                            int tempCurRoomID = Convert.ToInt32(tbxDutyAllocationCurrRoomID.Text);
                            hkr.RoomID = Convert.ToInt32(cbBoxDutyAllocationRoomID.Text);
                            hkr.HousekeepingDateTime = DateTime.MaxValue;
                            hkr.Duties = cbBoxDutyAllocationDuties.Text;
                            DBManager.UpdateHouseKeeperDuties(hkr, staffID);
                            r.RoomStatus = "Vacant";
                            r.RoomID = tempCurRoomID;
                            DBManager.UpdateRoomStatus(r);
                            r.RoomStatus = "Scheduled for Duty";
                            r.RoomID = Convert.ToInt32(cbBoxDutyAllocationRoomID.SelectedItem.ToString());
                            DBManager.UpdateRoomStatus(r);
                            DBManager.FillDropDownListRoomID(cbBoxDutyAllocationRoomID);
                            success = true;
                        }
                        
                        
                    }
                    else
                    {
                        MessageBox.Show("No Room Selected or Duty selected!");
                    }
                }
                if (success == true)
                {
                    MessageBox.Show("Duty has been updated.");
                    DBManager.ListViewListAllHousekeeperWithDuties(lvDutyAllocationHouseKeeperAllocation);
                    ClearTextBoxes(this.Controls);
                }
            }
            
        }

        private void btnDutyAllocationRefresh_Click(object sender, EventArgs e)
        {
            DBManager.ListViewListCheckForHousekeeper(lvDutyAllocationHouseKeeperAllocation);
        }
        private void btnStaffDetailsRecordSearch_Click(object sender, EventArgs e)
        {

            if (!string.IsNullOrEmpty(tbxStaffDetailsRecordSearchName.Text))
            {
                string name = tbxStaffDetailsRecordSearchName.Text;
                DBManager.ListViewStaffDetailRecordsByName(lvStaffDetailsRecord, name);
            }
            else if (!string.IsNullOrEmpty(tbxStaffDetailsRecordSearchID.Text))
            {
                int id = Convert.ToInt32(tbxStaffDetailsRecordSearchID.Text);
                DBManager.ListViewStaffDetailRecordsByID(lvStaffDetailsRecord, id);
            }
            else if (string.IsNullOrEmpty(tbxStaffDetailsRecordSearchName.Text) || string.IsNullOrEmpty(tbxStaffDetailsRecordSearchID.Text))
            {
                MessageBox.Show("Nothing to search!");
            }
        }
        #endregion

        #region Utilites
        // This method only allows numeric to be press.
        private void restrictCharacter_KeyPress(object sender, KeyPressEventArgs e)
        {
            const char Delete = (char)8;
            e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
        }

        // This method only allows either staffdetailrecord to only search through one name or ID.
        private void tbxStaffDetailsRecordSearchNameAndSearchID_TextChanged(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(tbxStaffDetailsRecordSearchName.Text))
            {
                tbxStaffDetailsRecordSearchID.Clear();
                tbxStaffDetailsRecordSearchName.Text = ((TextBox)sender).Text;
            }
            else if (!string.IsNullOrEmpty(tbxStaffDetailsRecordSearchID.Text))
            {
                tbxStaffDetailsRecordSearchName.Clear();
                tbxStaffDetailsRecordSearchID.Text = ((TextBox)sender).Text;
            }   
        }

        // THis method clears all the text boxes in the controls.
        private void ClearTextBoxes(Control.ControlCollection cc)
        {
            foreach (Control ctrl in cc)
            {
                TextBox tb = ctrl as TextBox;
                if (tb != null)
                {
                    tb.Text = "";
                }

                else
                {
                    ClearTextBoxes(ctrl.Controls);
                }
            }
        }
        #endregion     
    }
}